 window.onload = function() {  
     if (document.getElementsByTagName) {  
         var s = document.getElementsByTagName("select");  
   
         if (s.length > 0) {  
             window.select_current = new Array();  
   
             for (var i=0, select; select = s[i]; i++) {  
                 select.onfocus = function(){ window.select_current[this.id] = this.selectedIndex; }  
                 select.onchange = function(){ opt_restore(this); }  
                 opt_emulate(select);  
             }  
         }  
     }  
 }  
   
 function opt_restore(e) {  
     if (e.options[e.selectedIndex].disabled) {  
         e.selectedIndex = window.select_current[e.id];  
     }  
 }  
   
 function opt_emulate(e) {  
     for (var i=0, option; option = e.options[i]; i++) {  
         if (option.disabled) {  
             option.style.color = "graytext";  
         }  
         else {  
             option.style.color = "menutext";  
         }  
     }  
 }


var editorTools = 'Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,Img,Flash,Media,Emot,Table,|,Source,Fullscreen,About';

/**
 * 设置编辑器的高度
 */
function editorHeightReset(px, id) {
	var xheHeight = $('#'+id).height()+px;
	if(xheHeight > 1200 || xheHeight < 100) return false;
	$('#'+id).css('height', xheHeight+'px')
}


/**
 * 构建JSON格式数据对象
 *
 * usage：
 * var jb = new JsonBuilder();
 * jb.add('field A', 'str'); // 添加json key/value 项
 * jb.add('数据项 B', '内容'); // 添加json key/value 项
 * alert(jb.getJson()); // 获取所构建的json数据
 */
function JsonBuilder () {
	var data = new Object();
	
	/**
	 * 添加key/value数据项
	 * 
	 * @param key json数据项下标
	 * @param val json数据项的值
	 */
	this.add = function (key, val) {
		data[key] = val;
	}
	
	/**
	 * 获取json数据
	 */
	this.getJson = function () {
		var s = '';
		for(var k in data) {
			if(s.length > 0) {
				s += ',';
			}
						
			s += "'" + k.replace(/'/g, "\\\'") + "'" + ': \'' + data[k].replace(/'/g, "\\\'") + '\'';
		}
		
		return '{'+s+'}';
	}
}

/**
 * 在页面中创建弹出div窗口
 * 窗口样式在global样式中
 *
 * @param width 窗口宽 （默认560）
 * @param height 窗口高 （默认410）
 */
function creatPopBox(title, width, height) {
	if(typeof(title) == 'undefined') title = '提示信息';
	if(typeof(height) == 'undefined') height = 410;
	if(typeof(width) == 'undefined') width = 560;
	var top = ($(document).height() - height)/2;
	var left = ($(document).width() - width)/2;
	
	$('#pop-box, #pop-box-out').remove();
	
	var style = ' left:' + left +'px;'
	          + ' top:' + top + 'px;'
			  + ' height:' + height + 'px;'
			  + ' width:' + width + 'px;';
			  
	var html = '<div id="pop-box-out"></div>'
			 + '<div id="pop-box" style="'+style+'">'
			 + '  <a class="close"></a>'
			 + '  <div class="pop-box-in">'
			 + '    <div class="in-wrap">'
			 + '      <h1 class="pop-title">'+title+'</h1>'
			 + '      <div id="pop-main"><div class="floor-loading"></div></div>'
			 + '    </div>'
			 + '  </div>'
			 + '</div>'
	$('body').append(html);
	
	dragable('pop-box', '.pop-title');
	
	$('#pop-box .close').click(function(){$('#pop-box, #pop-box-out').remove();});
		
	$(window).resize(function(){
		if($('#pop-box').height() == null) return;
		
		var top = ($(window).height() - height)/2;
		var left = ($(window).width() - width)/2;
		
		$('#pop-box').css('left', left+'px');
		$('#pop-box').css('top', top+'px');	
	});
}

/**
 * 使层可拖动
 * 
 * @param string o 可拖动层的id
 * @param string moveTag 光标显示为可移动的元素， 可用id，类，标签，如#xx、.xx或xx 
 */
function dragable(o, moveTag) {  
    if (typeof o == "string") o = document.getElementById(o);  
    o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;  
    o.orig_y = parseInt(o.style.top) - document.body.scrollTop;  
    o.orig_index = o.style.zIndex;
	
	o.onmouseover = function (a) {
		$(moveTag).css('cursor', 'move');
	}
	
	o.onmouseout = function (a) {
 		$(moveTag).css('cursor', 'normal');
	}
	
    o.onmousedown = function(a) {  
        this.style.zIndex = 10000;
        var d  = document;
        if(!a) a = window.event;
        var x = a.clientX + d.body.scrollLeft - o.offsetLeft;
        var y = a.clientY + d.body.scrollTop - o.offsetTop;

        d.ondragstart = "return false;"
        d.onselectstart = "return false;"
        d.onselect = "document.selection.empty();"

        if(o.setCapture) {
            o.setCapture();
		} else if(window.captureEvents) {
            window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		}

        d.onmousemove = function(a) {
            if(!a) a = window.event;
            $('#'+o.id).css('left', a.clientX+document.body.scrollLeft-x + 'px');
            $('#'+o.id).css('top', a.clientY+document.body.scrollTop-y + 'px');
			
            o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
            o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
        }
		
        d.onmouseup = function() {
            if(o.releaseCapture) {
                o.releaseCapture();
			} else if(window.captureEvents) {
                window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			}

            d.onmousemove = null;
            d.onmouseup = null;
            d.ondragstart = null;
            d.onselectstart = null;
            d.onselect = null;
            o.style.cursor = "normal";
            o.style.zIndex = o.orig_index;
        }
    }
	
	var orig_scroll = window.onscroll ? window.onscroll:function (){};
	window.onscroll = function () {
		orig_scroll();
		o.style.left = o.orig_x + document.body.scrollLeft;
		o.style.top = o.orig_y + document.body.scrollTop;
	}
}
